package com.itheima.dao;

import com.itheima.domian.Student;
import com.itheima.until.JDBCUntil;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class StudentDaoImpl implements StudentDao {

    /*查询表中所有的数据*/
    Connection conn = null;
    PreparedStatement state = null;
    ResultSet rs = null;
    @Override
    public List<Student> AllFindStudent() {

        ArrayList<Student> studentList = new ArrayList<>();
        try {
            conn = JDBCUntil.getConnection();
            String sql = "select * from student";
            state = conn.prepareStatement(sql);
            rs = state.executeQuery();
            while (rs.next()) {
                Integer sid = rs.getInt("sid");
                String name = rs.getString("name");
                Integer age = rs.getInt("age");
                Date birthday = rs.getDate("birthday");
                Student student = new Student(sid, name, age, birthday);
                studentList.add(student);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUntil.close(conn, state, rs);
            return studentList;
        }
    }

    @Override
    public Student findStudent(int id) {
        try {
            conn = JDBCUntil.getConnection();
            String sql ="select * from student where sid =?";
            state = conn.prepareStatement(sql);
            state.setInt(1,id);
            ResultSet rs = state.executeQuery();

            Student student = null;
            while (rs.next()) {
                Integer sid = rs.getInt("sid");
                String name = rs.getString("name");
                Integer age = rs.getInt("age");
                Date birthday = rs.getDate("birthday");
                student = new Student(sid,name,age,birthday);
            }
            return student;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUntil.close(conn,state);

        }
      return null;
    }

    @Override
    public int addStudent(Student student) {
        try {
            conn = JDBCUntil.getConnection();
            String sql ="insert into student value(null,?,?,?)";
            PreparedStatement state = conn.prepareStatement(sql);
            java.util.Date date =  student.getBirthday();
            String birthday = new SimpleDateFormat("yyyy-MM-dd").format(date);
            state.setString(1,student.getName());
            state.setInt(2,student.getAge());
            state.setString(3,birthday);
            int i = state.executeUpdate();
            return  i;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUntil.close(conn,state);
        }
        return -1;
    }

    @Override
    public int updateStudent(Student student ,int id ) {
        try {
            conn = JDBCUntil.getConnection();
            String sql ="update student set  name =? ,age=?,birthday =? where sid =?";
            PreparedStatement state = conn.prepareStatement(sql);
            java.util.Date date = student.getBirthday();
            String birthday = new SimpleDateFormat("yyyy-MM-dd").format(date);
            state.setString(1,student.getName());
            state.setInt(2,student.getAge());
            state.setString(3,birthday);
            state.setInt(4,id);
            int i = state.executeUpdate();
            return i;

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUntil.close(conn,state);
        }
        return -1;
    }

    @Override
    public int deleteStudent(int id) {
        try {
            conn = JDBCUntil.getConnection();
            conn = JDBCUntil.getConnection();
            String sql ="delete from student where sid =?";
            PreparedStatement state = conn.prepareStatement(sql);
            state.setInt(1,id);
            int i = state.executeUpdate();
            return  i;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUntil.close(conn,state);
        }
        return -1;
    }


}